import 'package:web_socket_channel/io.dart';
import 'dart:convert';

class MySocket {
  IOWebSocketChannel? _channel;
  final websocketConnectionUri = 'http://geek.itheima.net';

  MySocket() {
    initSocket();
  }

  /// 接收信息
  void _onData(data) {
    print('接收数据: $data');
  }

  /// 连接错误
  void _onError(err) {
    print('连接错误：$err');
    _channel?.sink.close();
  }

  /// 连接断开
  void _onDone() {
    // _isConnect = false;
    print('连接断开');
  }

  initSocket() async {
    print('开始连接');
    _channel = IOWebSocketChannel.connect(Uri.parse(websocketConnectionUri));
    _channel!.stream.listen(
      _onData,
      onError: _onError,
      onDone: _onDone,
    );
    // _isConnect = true;
    print('连接成功');
  }

  socketSend(dynamic value) {}

  socketClose() async {}
}
